package guida.ccf.gzuexaminesystem.controller;

import guida.ccf.gzuexaminesystem.common.Bucket;
import guida.ccf.gzuexaminesystem.common.HttpStatus;
import guida.ccf.gzuexaminesystem.common.HttpStatusDescriptionsImpl;
import guida.ccf.gzuexaminesystem.common.ResponseResult;
import guida.ccf.gzuexaminesystem.dto.StudentSaveFileDto;
import guida.ccf.gzuexaminesystem.entity.minio.DeleteFileDto;
import guida.ccf.gzuexaminesystem.service.StudentAwardService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ccf
 * @since 2023-11-03
 */
@RestController
@RequestMapping("/studentAward")
public class StudentAwardController {

    @Autowired
    private StudentAwardService studentAwardService;

    @Autowired
    private HttpStatusDescriptionsImpl descriptions;

    /**
     * 个人项目上传
     * md5 原始文件的md5值
     * @return true 成功, false 失败
     */
    @PreAuthorize("hasAnyAuthority(T(guida.ccf.gzuexaminesystem.expression.AuthorityUtils)" +
            ".studentUploadFile(#fileDto, #token))")
    @ApiOperation("文件上传接口")
    @PostMapping(value = "/personal/saveFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult<?> upload(StudentSaveFileDto fileDto, @RequestHeader("token") String token) {

        MultipartFile[] files = fileDto.getFiles();
        String[] md5s = fileDto.getMd5s();

        // 设置存储路径
        String folder = fileDto.getProjectName() + "/" + fileDto.getStudentId() + "/" + fileDto.getAwardTime() + "/";
        fileDto.setFolder(folder);

        // 参数校验: md5数组长度要和文件数组长度对应
        if (files.length != md5s.length) {
            return ResponseResult.fail("网络不佳,上传失败!");
        }

        return studentAwardService.saveFile(fileDto, files, md5s);
    }

    /**
     * 学生传入相关参数获得文件
     * @param folder 文件夹
     * @param fileName  文件名
     * @param md5  原始文件的md5值
     * @param response 响应
     */
    @PreAuthorize("@ex.hasAnyAuthority(#folder, #token)")
    @GetMapping("/getFile")
    @ApiOperation("文件下载接口")
    public String getFile(@RequestParam String folder,
                          @RequestParam String fileName,
                          @RequestParam String md5,
                          @RequestHeader(value = "token") String token,
                          HttpServletResponse response) {

        return studentAwardService.getFile(Bucket.STUDENT_BUCKET,folder,fileName,md5,response);

    }


//    /**
//     * 学生删除单个文件
//     * @return true 成功, false 失败
//     */
//    @PreAuthorize(
//            "hasAnyAuthority(T(guida.ccf.gzuexaminesystem.expression.AuthorityUtils)" +
//                    ".getAuthoritiesByList(#list, #token, '" + Bucket.STUDENT_BUCKET + "'))"
//    )
//    @PostMapping("/deleteFile")
//    @ApiOperation("文件删除接口")
//    public ResponseResult<?> deleteFile(@RequestBody ArrayList<DeleteFileDto> list, @RequestHeader("token") String token){
//
//        return studentAwardService.deleteFileAll(list);
//
//    }


    /**
     *  学生获取审核中的奖项列表.
     */
    @ApiOperation(value = "学生获取审核中的奖项", notes = "参数为学号")
    @GetMapping("/getUnderReviewAward/{studentId}")
    @PreAuthorize(
            "hasAnyAuthority(T(guida.ccf.gzuexaminesystem.expression.AuthorityUtils)" +
                    ".commonAuthorities(#studentId, #token))"
    )
    public ResponseResult<?> getUnderReviewAward(@PathVariable("studentId") String studentId,
                                                 @RequestHeader("token") String token) {

        return studentAwardService.getUnderReviewAward(studentId);
    }


    /**
     *  学生获取审核失败的奖项列表.
     */
    @ApiOperation(value = "学生获取审核失败的奖项", notes = "参数为学号")
    @GetMapping("/getReviewAwardPassed/{studentId}")
    @PreAuthorize(
            "hasAnyAuthority(T(guida.ccf.gzuexaminesystem.expression.AuthorityUtils)" +
                    ".commonAuthorities(#studentId, #token))"
    )
    public ResponseResult<?> getReviewAwardPassed(@PathVariable("studentId") String studentId,
                                                 @RequestHeader("token") String token) {

        return studentAwardService.getReviewAwardPassed(studentId);
    }


    /**
     *  学生获取审核失败的奖项列表.
     */
    @ApiOperation(value = "学生获取审核失败的奖项", notes = "参数为学号")
    @GetMapping("/getReviewAwardFailed/{studentId}")
    @PreAuthorize(
            "hasAnyAuthority(T(guida.ccf.gzuexaminesystem.expression.AuthorityUtils)" +
                    ".commonAuthorities(#studentId, #token))"
    )
    public ResponseResult<?> getReviewAwardFailed(@PathVariable("studentId") String studentId,
                                                  @RequestHeader("token") String token) {

        return studentAwardService.getReviewAwardFailed(studentId);
    }


    /**
     * 学生的我的奖项模块: 包含个人和团队
     */


}
